package com.ruoyi.gd.todo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.gd.ywdwgd.domain.ZYwdwgd;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gd.todo.domain.bo.zTodoBo;
import com.ruoyi.gd.todo.domain.vo.zTodoVo;
import com.ruoyi.gd.todo.domain.zTodo;
import com.ruoyi.gd.todo.mapper.zTodoMapper;
import com.ruoyi.gd.todo.service.IzTodoService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 待办内容Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-19
 */
@RequiredArgsConstructor
@Service
public class zTodoServiceImpl implements IzTodoService {

    private final zTodoMapper baseMapper;

    @Resource
    private ISysRoleService sysRoleService;

    @Resource
    private ISysUserService sysUserService;

    /**
     * 查询待办内容
     */
    @Override
    public zTodoVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询待办内容列表
     */
    @Override
    public TableDataInfo<zTodoVo> queryPageList(zTodoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<zTodo> lqw = buildQueryWrapper(bo);
        Page<zTodoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询待办内容列表
     */
    @Override
    public List<zTodoVo> queryList(zTodoBo bo) {
        LambdaQueryWrapper<zTodo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<zTodo> buildQueryWrapper(zTodoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<zTodo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), zTodo::getTitle, bo.getTitle());
        lqw.eq(bo.getQssj() != null, zTodo::getQssj, bo.getQssj());
        lqw.eq(StringUtils.isNotBlank(bo.getQsrid()), zTodo::getQsrid, bo.getQsrid());
        lqw.eq(StringUtils.isNotBlank(bo.getQsr()), zTodo::getQsr, bo.getQsr());
        lqw.eq(bo.getWcsj() != null, zTodo::getWcsj, bo.getWcsj());
        lqw.eq(bo.getPriority() != null, zTodo::getPriority, bo.getPriority());
        lqw.eq(StringUtils.isNotBlank(bo.getFjid()), zTodo::getFjid, bo.getFjid());
        lqw.eq(StringUtils.isNotBlank(bo.getBz()), zTodo::getBz, bo.getBz());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateId()), zTodo::getCreateId, bo.getCreateId());
        // 当前状态查询
        if (StringUtils.isNotEmpty(bo.getStatus())) {
            List<String> strings = Arrays.asList(bo.getStatus().split(","));
            lqw.in(zTodo::getStatus, strings);
        }
        // 重要任务
        lqw.orderByDesc(zTodo::getPriority);
        // 判断是不是领导的菜单
        if(params.size() > 0) {
            String syrw = (String) params.get("syrw");
            if (StringUtils.isNotEmpty(syrw) && "true".equals(syrw)) {
                lqw.orderByDesc(zTodo::getStatus);
            }
        }
        // 创建时间
        lqw.orderByDesc(zTodo::getCreateTime);
        return lqw;
    }

    /**
     * 新增待办内容
     */
    @Override
    public Boolean insertByBo(zTodoBo bo) {
        zTodo add = BeanUtil.toBean(bo, zTodo.class);
        validEntityBeforeSave(add);
        String id = IdUtil.simpleUUID();
        add.setId(id);
        LoginUser loginUser = LoginHelper.getLoginUser();
        add.setCreateId(loginUser.getUserId()+"");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改待办内容
     */
    @Override
    public Boolean updateByBo(zTodoBo bo) {
        zTodo update = BeanUtil.toBean(bo, zTodo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(zTodo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除待办内容
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 修改状态
     * @param bo
     * @return
     */
    @Override
    public Boolean updateStatus(zTodoBo bo) {
        return baseMapper.updateStatus(bo) > 0;
    }

    @Override
    public List<SysUser> queryWyUser() {
        // 查询文员角色
        SysRole sysRole = sysRoleService.queryWyUser();
        SysUser sysUser = new SysUser();
        sysUser.setRoleId(sysRole.getRoleId());
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(1);
        pageQuery.setPageNum(1000);
        // 通过角色id查询当前的用户
        TableDataInfo<SysUser> sysUserTableDataInfo = sysUserService.selectAllocatedList(sysUser, pageQuery);
        return sysUserTableDataInfo.getRows();
    }

    /**
     * 重置任务
     * @param id
     * @return
     */
    @Override
    public Boolean resetTodo(String id) {
        return baseMapper.resetTodo(id) > 0;
    }
}
